"use strict";
cc._RF.push(module, '8900bVorUpD46tg/hc50nZk', 'LayerManager');
// script/Core/Libs/LayerManager.ts

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LayerType = void 0;
var Config_1 = require("../../Config/Config");
var ResLoader_1 = require("./ResLoader");
//ui层类型定义
var LayerType;
(function (LayerType) {
    LayerType[LayerType["Screen"] = 1] = "Screen";
    LayerType[LayerType["Fixed"] = 2] = "Fixed";
    LayerType[LayerType["Popup"] = 4] = "Popup";
    LayerType[LayerType["Toast"] = 8] = "Toast";
    LayerType[LayerType["Alone"] = 16] = "Alone";
})(LayerType = exports.LayerType || (exports.LayerType = {}));
var LayerManager = /** @class */ (function () {
    function LayerManager() {
        //layer缓存
        this.cacheLayer = new Map();
        //正在打开的layer缓存
        this.openingLayerName = [];
    }
    //设置根节点
    LayerManager.prototype.setRoot = function (root) {
        this.root = root;
    };
    //打开弹窗
    LayerManager.prototype.open = function (name, data) {
        return __awaiter(this, void 0, Promise, function () {
            var layerDesc, uiScript, _a, cacheLayerNames, _b, _c, _i, k, n, index;
            return __generator(this, function (_d) {
                switch (_d.label) {
                    case 0:
                        layerDesc = Config_1.UILayerConfig[name] || null;
                        if (!layerDesc) {
                            throw new Error("OpenUI[" + name + "] 不存在UI配置");
                        }
                        //检测是否正在打开中
                        if (this.openingLayerName.includes(name)) {
                            return [2 /*return*/];
                        }
                        this.openingLayerName.push(name);
                        if (this.cacheLayer.has(name)) {
                            uiScript = this.cacheLayer.get(name);
                        }
                        //如果已加载且已显示
                        if (uiScript && uiScript.node.active) {
                            console.log("layer", name, "已显示");
                            return [2 /*return*/, uiScript];
                        }
                        if (!layerDesc.loadingLayerName) return [3 /*break*/, 2];
                        return [4 /*yield*/, this.open(layerDesc.loadingLayerName)];
                    case 1:
                        _d.sent();
                        _d.label = 2;
                    case 2:
                        _a = Config_1.UILayerConfig[name].type;
                        switch (_a) {
                            case LayerType.Screen: return [3 /*break*/, 3];
                            case LayerType.Fixed: return [3 /*break*/, 10];
                            case LayerType.Popup: return [3 /*break*/, 11];
                            case LayerType.Toast: return [3 /*break*/, 12];
                            case LayerType.Alone: return [3 /*break*/, 13];
                        }
                        return [3 /*break*/, 16];
                    case 3:
                        cacheLayerNames = Array.from(this.cacheLayer.keys());
                        _b = [];
                        for (_c in cacheLayerNames)
                            _b.push(_c);
                        _i = 0;
                        _d.label = 4;
                    case 4:
                        if (!(_i < _b.length)) return [3 /*break*/, 7];
                        k = _b[_i];
                        n = cacheLayerNames[k];
                        if (!(Config_1.UILayerConfig[n].type == LayerType.Screen && this.cacheLayer.get(n).node.active)) return [3 /*break*/, 6];
                        return [4 /*yield*/, this.close(n)];
                    case 5:
                        _d.sent();
                        _d.label = 6;
                    case 6:
                        _i++;
                        return [3 /*break*/, 4];
                    case 7:
                        if (!!uiScript) return [3 /*break*/, 9];
                        return [4 /*yield*/, this.load(name, "Screen")];
                    case 8:
                        uiScript = _d.sent();
                        _d.label = 9;
                    case 9: return [3 /*break*/, 16];
                    case 10: return [3 /*break*/, 16];
                    case 11: return [3 /*break*/, 16];
                    case 12: return [3 /*break*/, 16];
                    case 13:
                        if (!!uiScript) return [3 /*break*/, 15];
                        return [4 /*yield*/, this.load(name, "Alone")];
                    case 14:
                        uiScript = _d.sent();
                        _d.label = 15;
                    case 15: return [3 /*break*/, 16];
                    case 16:
                        uiScript.node.active = true;
                        if (!layerDesc.loadingLayerName) return [3 /*break*/, 18];
                        return [4 /*yield*/, this.close(layerDesc.loadingLayerName)];
                    case 17:
                        _d.sent();
                        _d.label = 18;
                    case 18: return [4 /*yield*/, uiScript.showEffect()];
                    case 19:
                        _d.sent();
                        return [4 /*yield*/, uiScript.onShow(data)];
                    case 20:
                        _d.sent();
                        index = this.openingLayerName.indexOf(name);
                        if (index > -1) {
                            this.openingLayerName.splice(index, 1);
                        }
                        return [2 /*return*/, uiScript];
                }
            });
        });
    };
    //关闭弹窗
    LayerManager.prototype.close = function (name) {
        return __awaiter(this, void 0, void 0, function () {
            var uiScript;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (!Config_1.UILayerConfig[name]) {
                            throw new Error("OpenUI[" + name + "] 不存在UI配置");
                        }
                        uiScript = this.cacheLayer.get(name);
                        if (!uiScript) {
                            return [2 /*return*/];
                        }
                        //显示效果
                        return [4 /*yield*/, uiScript.hideEffect()];
                    case 1:
                        //显示效果
                        _a.sent();
                        return [4 /*yield*/, uiScript.onHide()];
                    case 2:
                        _a.sent();
                        uiScript.node.active = false;
                        console.log("close", name);
                        return [2 /*return*/];
                }
            });
        });
    };
    LayerManager.prototype.remove = function (name) {
        return __awaiter(this, void 0, Promise, function () {
            var uiScript, perfabAsset;
            return __generator(this, function (_a) {
                if (!this.cacheLayer.has(name))
                    return [2 /*return*/];
                uiScript = this.cacheLayer.get(name);
                perfabAsset = uiScript.node["__asset__"];
                perfabAsset.decRef();
                uiScript.node.destroy();
                this.cacheLayer.delete(name);
                return [2 /*return*/];
            });
        });
    };
    //加载方法
    LayerManager.prototype.load = function (name, layerNodeName) {
        return __awaiter(this, void 0, Promise, function () {
            var uiConfig, perfabNode, uiScript;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        uiConfig = Config_1.UILayerConfig[name];
                        return [4 /*yield*/, ResLoader_1.default.load(uiConfig.prefabUrl, cc.Prefab, uiConfig.bundleName).then(function (asset) {
                                //引用计数
                                asset.addRef();
                                //初始化节点 并在节点上 设置当前资源
                                var node = cc.instantiate(asset);
                                node["__asset__"] = asset;
                                return node;
                            })
                            //默认不显示
                        ];
                    case 1:
                        perfabNode = _a.sent();
                        //默认不显示
                        perfabNode.active = false;
                        uiScript = perfabNode.getComponent(uiConfig.scriptName);
                        //将UI添加进缓存
                        this.cacheLayer.set(name, uiScript);
                        //添加节点到相关节点
                        this.root.getChildByName(layerNodeName).addChild(perfabNode);
                        return [2 /*return*/, uiScript];
                }
            });
        });
    };
    LayerManager.instance = new LayerManager();
    return LayerManager;
}());
exports.default = LayerManager.instance;

cc._RF.pop();